/**
 * @class common.base.view
 * @extends common.base.object
 * 视图基类
 */
define(function (require) {
    require('routie');
    require('validate');

    var Base = require('common/base/object');

    return Base.extend({
        handles: {},
        initialize: function (params) {
            Base.prototype.initialize.apply(this, [params]);
            if (this.routers) {
                var routers = {};
                for (var k in this.routers) {
                    if (_.isFunction(this.routers[k])) {
                        routers[k] = this.routers[k];
                    } else {
                        routers[k] = this[this.routers[k]];
                    }
                }
                routie(routers, this);
            }
            this.parseTab();
        },
        setAppContent: function (tpl, params) {
            $('.app_content').html(this.render(_.isFunction(tpl) ? tpl() : tpl, params));
        },
        parseTab: function () {
            var _this = this;
            //处理nav-tabs
            _this.$('.JNavTabs a').each(function () {
                var data = $(this).data();
                $(this).attr('href', '#' + _.http_build_query(data));
            });
        },
        handleRouter: function (args) {
            var _this = this,
                isTabActive = false,
                hash = _.parse_hash(args);
            //处理nav-tabs
            _this.$('.JNavTabs a').each(function () {
                if (isTabActive)return;
                var data = $(this).data(),
                    dataKeys = _.keys(data),
                    params = [hash],
                    res = _.pick.apply(_this, params.concat(dataKeys));

                _.each(data, function (val, key) {
                    data[key] = val.toString();
                });
                if (_.isEqual(res, data)) {
                    isTabActive = true;
                    $(this).parents('li').addClass('active').siblings().removeClass('active');
                }
            });
        },
        loadHandles: function () {
            var _this = this;
            _.each(_this.handles, function (val) {
                new val({
                    wrap: _.empty(_this.el) ? $('body') : $(_this.el)
                });
            });
        },
        routie: routie
    });
});